<template>
  <div class="login-container">
    <div class="login-box">
      <!-- 头像区域 -->
      <div class="avatar-box">
        <img class="avatar" src="../assets/logo.png" />
      </div>
      <!-- 表单区域 -->
      <div class="form-login">
        <!-- 登录名称 -->
        <div class="form-group">
          <label for="username">登录名称：</label>
          <input
            type="text"
            class="form-control"
            id="username"
            placeholder="请输入登录名称"
            autocomplete="off"
            v-model.trim="username"
          />
        </div>
        <!-- 登录密码 -->
        <div class="form-group">
          <label for="password">登录密码：</label>
          <input
            type="password"
            class="form-control"
            id="password"
            placeholder="请输入登录密码"
            v-model.trim="password"
          />
        </div>
        <!-- 登录按钮 -->
      <div class="form-group">
        <button type="button" class="btn" @click="submit">{{title1}}</button>
      </div>
      <button style="margin-top: 20px;" @click="btn">{{title2}}</button>
      <br>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref  } from 'vue'
import { register, login } from '../api/userList'

import { useRouter } from 'vue-router'
const router = useRouter()
const username = ref('')
const password = ref('')
const title1 = ref('登录')
const title2 = ref('还没有账号')
const btn = () => {
  if (title1.value == '登录') {
    title1.value = '注册'
    title2.value = '已有账号'
  } else {
    title1.value = '登录'
    title2.value = '还没有账号'


  }
}
const submit = () => {
  title1.value == '登录' ? onLogin() : onregister()
}
const onLogin = async () => {
  login({
    username: username.value,
    password: password.value
  }).then(res => {
    console.log('res', res)
    if (res.token) {
      localStorage.setItem('userinfo', JSON.stringify(res.user))
      router.push({ name: 'studentList' })
    } else {
      alert('用户名或密码输入错误')
    }
  }).catch (res => {
      alert('用户名或密码输入错误')
  })
}
const onregister = async () => {
  const res = await register({
    username: username.value,
    password: password.value
  })
  if (res.token) {
    alert('注册成功，自动登录')

    onLogin()
  } else {
    alert('注册失败')
  }
}
</script>

<style scoped>
.login-container {
  background-color: #35495e;
  height: 100%;
}
.login-container .login-box {
  width: 400px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  box-shadow: 0 0 6px rgba(255, 255, 255, 0.5);
}
.login-container .login-box .form-login {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  box-sizing: border-box;
  padding: 1.5rem !important;
}
.login-container .login-box .form-login .form-group {
  padding-top: 20px;
}
.login-container .login-box .form-login .form-group .btn {
  background-color: deepskyblue;
  color: #fff;
}
.form-control {
  flex: 1;
  padding: 0.5rem;
}
.avatar-box {
  position: absolute;
  width: 100%;
  top: -65px;
  left: 0;
}
.avatar-box .avatar {
  width: 120px;
  height: 120px;
  border-radius: 50% !important;
  box-shadow: 0 0 6px #efefef;
  background-color: #fff;
}
</style>